﻿
using System;
using Unity.VisualScripting;
using UnityEngine;

[RequireComponent(typeof(Rigidbody2D))]
[RequireComponent(typeof(Animator))]
public class Actor : MonoBehaviour
{
    #region Unity 方法
    protected virtual void Awake()
    {
        InitPhysicsComponent();
        InitAnimationComponent();
        InitDirection();
    }
    private void Start()
    {
        _direction = -1;
    }
    protected virtual void Update()
    {
        _dis = _fontRadius * _direction;
    }
    protected virtual void OnDrawGizmos()
    {
        DrawSensorLine();
    }

    #endregion
    #region 物理系统
    protected Rigidbody2D _rb;

    public void InitPhysicsComponent()
    {
        _rb = GetComponent<Rigidbody2D>();
    }
    public void SetVelocity(float speed, Vector2 angle)
    {
        var dir = angle.normalized;
        _rb.velocity = speed * dir;
    }

    public void SetVelocityX(float xVelocity)
    {
        _rb.velocity = new Vector2(xVelocity, _rb.velocity.y);
    }

    public void SetVelocityY(float yVelocity)
    {
        _rb.velocity = new Vector2(_rb.velocity.x, yVelocity);
    }

    public void SetVelocityZero()
    {
        _rb.velocity = Vector2.zero;
    }
    public void SetGravityScale(float value) 
    {
        _rb.gravityScale = value;
    }

    #endregion
    #region 动画
    protected Animator _animator;
    private void InitAnimationComponent()
    {
        _animator = GetComponent<Animator>();
    }

    public void PlayAnim(string animName)
    {
        _animator.Play(animName);
    }
    #endregion
    #region 翻转缩放
    protected int _direction;
    protected virtual void InitDirection()
    {
        _direction = -1;
    }
    public void ShouldIfFlip(int inputX) 
    {
        if (inputX == 0||inputX==_direction) 
        {
            return;
        }
        Flip();
    }
    public void Flip() 
    {
        _direction = -_direction;
        transform.Rotate(0,-180,0);
    }

    #endregion
    #region 检测器
    [SerializeField] private Transform _groundSensor;
    [SerializeField] private float _groundRadius;
    [SerializeField]private LayerMask _groundMask;
    
    public bool OnGrounded => Physics2D.OverlapCircle(_groundSensor.position, _groundRadius, _groundMask);


    [SerializeField] private Transform _fontSensor;
    [SerializeField] private float _fontRadius;
    [SerializeField] private LayerMask _fontMask;
    public bool HasThingFont => Physics2D.Linecast(_fontSensor.position, new Vector2(_fontSensor.position.x + _direction * _fontRadius, _fontSensor.position.y),_fontMask);
    public Collider2D Obstacle => Physics2D.Linecast(_fontSensor.position, new Vector2(_fontSensor.position.x + _direction * _fontRadius, _fontSensor.position.y), _fontMask).collider;


    [SerializeField] private Transform _downSensor;
    [SerializeField] private float _downRadius;
    [SerializeField] private LayerMask _downMask;

    public bool HasThingDown => Physics2D.Linecast(_downSensor.position, new Vector2(_downSensor.position.x,_downSensor.position.y-_downRadius), _downMask);
  
    float _dis = -1;
    protected virtual void DrawSensorLine() 
    {
        Gizmos.DrawWireSphere(_groundSensor.position, _groundRadius);
        Gizmos.DrawLine(_fontSensor.position,_fontSensor.position-transform.right*_fontRadius);
        Gizmos.DrawLine(_downSensor.position, new Vector2(_downSensor.position.x, _downSensor.position.y - _downRadius));
    }
    #endregion

}
